package presidents.game;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;

public class Deck {
	private ArrayList<Card> deck;
	private Type[] typeList;
	private int cardsInDeck;
	
	public Deck(boolean graphical){
		typeList = new Type[TYPES_PER_DECK];
		typeList[0] = Type.CLUB;
		typeList[1] = Type.DIAMOND;
		typeList[2] = Type.HEART;
		typeList[3] = Type.SPADE;
		cardsInDeck = CARDS_IN_DECK;
		
		if(graphical){
			initGraphicalDeck();
		}
		else initDeck();
	}
	
	private void initGraphicalDeck() {
		// TODO Create a graphical version
		
	}

	private void initDeck() {
		
		System.out.println("Initializing deck...");
		deck = new ArrayList<Card>();
		for (int i = 0; i < CARDS_PET_TYPE; i++) {
			for (int j = 0; j < typeList.length; j++) {
				deck.add(new Card(i, typeList[j]));
			}
		}
		System.out.println("Deck initialized");
	}
	
	public void printDeck(){
		for (Card card: deck) {
			card.printCard();
		}
	}
	
	public int cardsInDeck(){
		return cardsInDeck;
	}
	
	public boolean removeCard(Card card){
		if(deck.remove(card)){
			cardsInDeck--;
			return true;
		} else return false;
	}
	
	public Card getCard(int cardNr, Type cardType){
		boolean found = false;
		Card tempCard = null;
		for (Card card: deck) {
			if(card.getNr() == cardNr && card.getType() == cardType){
				found = true;
				tempCard = card;
			}
		}
		if(!found){
			System.out.println("Card not found!");
			return null;
		} else return tempCard;
	}
	
	public Card getCard(int index){
		return deck.get(index);
	}
	
	public ArrayList<Card> getDeck(){
		return deck;
	}
	
	public Type getType(int typeIndex){
		return typeList[typeIndex];		
	}
	
	public void shuffle(){
		ArrayList<Card> tempDeck = new ArrayList<Card>();
		Random generator = new Random();
		for (int i = 0; i < CARDS_IN_DECK; i++) {
			tempDeck.add(deck.remove(generator.nextInt(deck.size())));
		}
		deck = tempDeck;
		System.out.println("Deck shuffled!");
	}
	
	private static int CARDS_IN_DECK = 52;
	private static int CARDS_PET_TYPE = 13;
	private static int TYPES_PER_DECK = 4;
}
